/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/

package com.jinhe.tss.core.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import com.jinhe.tss.core.exception.BusinessException;

/** 
 * <p> JarFileHelper.java </p> 
 * 
 */
public class JarFileHelper {
    
    /**
     * 从jar文件中拷贝一个指定的文件到classes上一层临时temp目录下
     * @param jarFileName
     * @param entryName
     */
    public static void copyFileFromJar(String jarFileName, String entryName){ 
        String contentPath = Thread.currentThread().getContextClassLoader().getResource("").getFile();
        copyFileFromJar(new File(contentPath + "temp/"), jarFileName, entryName);
    }
    
    /**
     * 从jar文件中拷贝一个指定的文件到指定目录下
     * @param dir
     * @param jarFile
     * @param entry
     */
    public static void copyFileFromJar(File dir, String jarFileName, String entryName){ 
        String contentPath = Thread.currentThread().getContextClassLoader().getResource("").getFile();
        File file = new File(contentPath.substring(0, contentPath.length() - 8) + "lib/" + jarFileName);
        JarFile jarFile;
        try {
            jarFile = new JarFile(file);
        } catch (IOException e) {
            throw new BusinessException("读取jar文件时出错!", e);
        }
        JarEntry entry = jarFile.getJarEntry(entryName);
        
        if(!dir.exists()){
            dir.mkdir();   
        }
        String fileName = entry.getName();
        File newFile = new File(dir.getPath() + "/" + fileName);
        InputStream in = null;
        OutputStream out = null;
        try {
            newFile.getParentFile().mkdirs();
            in = jarFile.getInputStream(entry);
            out = new FileOutputStream(newFile);
            
            int len = 0;
            byte[] b = new byte[1024];           
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
                out.flush();
            }   
            in.close();
        } catch (IOException e) {
            throw new BusinessException("复制jar包中文件时出错!", e);
        }finally{
            try {
                if(in != null)
                    in.close();
                if(out != null)
                    out.close();
                if(jarFile != null)
                    jarFile.close();
            } catch (IOException e) {
                throw new BusinessException("关闭jar文件输出时出错!", e);
            }
        }
    }
    
    public static void main(String[] args) throws IOException{
        JarFileHelper.copyFileFromJar("tss-dynproperty-src.jar", "com/jinhe/tss/component/dynproperty/support/AbstractDynEntity.java");
    }
}

